package com.sunky.rfidapp

import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.sunky.db.model.*
import com.sunky.rfidapp.util.CommonUtil
import com.sunky.rfidapp.util.setTextValid
import kotlinx.android.synthetic.main.activity_chiper_info_detail.*
import kotlinx.android.synthetic.main.activity_equip_info_detail.*
import kotlinx.android.synthetic.main.layout_title.*

/**
 * 装备信息详情
 */
class InfoDetailActivity : BaseActivity() {
    //密码设备标识
    var isChiper = false

    var equipInfo: EquipInfo? = null

    var chiperInfo: ChiperEquipInfo? = null

    override fun handleIntent(intent: Intent) {
        isChiper = intent.getBooleanExtra("ISCHIPER", false)
        if (isChiper) {
            chiperInfo = CommonUtil.GlobalStorage["InfoDetailActivity"] as ChiperEquipInfo
        } else {
            equipInfo = CommonUtil.GlobalStorage["InfoDetailActivity"] as EquipInfo
        }
        CommonUtil.GlobalStorage.remove("InfoDetailActivity")

    }

    override fun getLayoutId(): Int {
        return if (isChiper) R.layout.activity_chiper_info_detail else R.layout.activity_equip_info_detail
    }

    override fun initView() {
        TvTitle.text = "装备信息"
    }

    override fun initData() {
        equipInfo?.apply {
            TvTag.setTextValid(codeInfo)
            TvName.setTextValid(equipName)
            TvCount.setTextValid(equipCount)
            TvUnit.setTextValid(measureUnit)
            TvProducer.setTextValid(equipProducer)
            TvSerial.setTextValid(equipSerialNo)
            TvWeight.setTextValid(equipWeight)
            TvSize.setTextValid(equipSize)
            TvSerialNo.setTextValid(serialNo)
            TvUnitName.setTextValid(unitName)
            TvProducerDate.setTextValid(producerDate)
            TvIssueDate.setTextValid(issueDate)
            TvCarNo.setTextValid(carNo)
            TvBottomNo.setTextValid(bottomNo)
            TvFunction.setTextValid(mainFunction)
            TvQuato.setTextValid(mainQuato)
            if (TextUtils.isEmpty(mainFunction) && TextUtils.isEmpty(mainQuato)) {
                TvAreaUsage.visibility = View.GONE
            }
            val inflater = LayoutInflater.from(this@InfoDetailActivity)
            var gson = Gson()
            if (!TextUtils.isEmpty(mainEquip) && "[]" != mainEquip) {
                TvAreaMainEquip.visibility = View.VISIBLE
                LayoutMainEquip.visibility = View.VISIBLE
                var mainEquips = gson.fromJson<List<MainEquipBean>>(mainEquip, object : TypeToken<List<MainEquipBean>>() {}.type)
                mainEquips.sortedWith(kotlin.Comparator { t1, t2 -> t1.order.compareTo(t2.order) }).forEach {
                    var view = inflater.inflate(R.layout.item_main_equip, LayoutMainEquip, false)
                    LayoutMainEquip.addView(view)
                    view.findViewById<TextView>(R.id.TvEquipName).text = it.equipName
                    view.findViewById<TextView>(R.id.TvEquipProducer).text = it.equipProducer
                    view.findViewById<TextView>(R.id.TvEquipProduceDate).text = it.produceDate
                    view.findViewById<TextView>(R.id.TvEquipCount).text = it.equipCount
                }
            }
            if (!TextUtils.isEmpty(classType) && "[]" != classType) {
                TvAreaServiceType.visibility = View.VISIBLE
                LayoutServiceType.visibility = View.VISIBLE
                var classTypes = gson.fromJson<List<ClassTypeBean>>(classType, object : TypeToken<List<ClassTypeBean>>() {}.type)
                classTypes.sortedWith(kotlin.Comparator { t1, t2 -> t1.order.compareTo(t2.order) }).forEach {
                    var view = inflater.inflate(R.layout.item_business_type, LayoutServiceType, false)
                    LayoutServiceType.addView(view)
                    view.findViewById<TextView>(R.id.TvServiceType).text = it.serviceName
                    view.findViewById<TextView>(R.id.TvServiceCount).text = it.serviceCount
                }
            }
            if (!TextUtils.isEmpty(userInterface) && "[]" != userInterface) {
                TvAreaInterface.visibility = View.VISIBLE
                LayoutInterfaceType.visibility = View.VISIBLE
                var userInterface = gson.fromJson<List<UserInterfaceBean>>(userInterface, object : TypeToken<List<UserInterfaceBean>>() {}.type)
                userInterface.sortedWith(kotlin.Comparator { t1, t2 -> t1.order.compareTo(t2.order) }).forEach {
                    var view = inflater.inflate(R.layout.item_interface, LayoutInterfaceType, false)
                    LayoutInterfaceType.addView(view)
                    view.findViewById<TextView>(R.id.TvInterfaceName).text = it.interfaceName
                    view.findViewById<TextView>(R.id.TvInterfaceCount).text = it.interfaceCount
                }
            }
        }

        chiperInfo?.apply {
            TvCTag.setTextValid(codeinfo)
            TvCName.setTextValid(equipName)
            TvCOldName.setTextValid(equipOldName)
            TvCCount.setTextValid(equipCount)
            TvCUnit.setTextValid(equipUnit)
            TvCSerialNo.setTextValid(equipSerialNo)
            TvCProducer.setTextValid(equipProducer)
            TvCLeaveDate.setTextValid(leaveDate)
            TvCIssueDate.setTextValid(issueDate)
            TvCAttachment.setTextValid(attachment)
            TvCState.setTextValid(equipState)
            TvCPlatformName.setTextValid(platformName)
            TvCUnitName.setTextValid(unitName)
            TvCResponsePerson.setTextValid(responsePerson)
            TvCMemo.setTextValid(memo)
        }
    }
}